﻿
namespace SharpTalk.Client.WpfApplication.Controls
{
    using System.Windows.Controls;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;

    /// <summary>
    /// Interaction logic for TextBoxButton.xaml
    /// </summary>
    public partial class TextBoxButton : UserControl
    {
        public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(TextBoxButton));
        public static DependencyProperty ButtonContentProperty = DependencyProperty.Register("ButtonContent", typeof(object), typeof(TextBoxButton));
        public static DependencyProperty ButtonCommandProperty = DependencyProperty.Register("ButtonCommand", typeof(ICommand), typeof(TextBoxButton));
        public static DependencyProperty InsideBorderCornerRadiusProperty = DependencyProperty.Register("InsideBorderCornerRadius", typeof(CornerRadius), typeof(TextBoxButton));
        public static DependencyProperty InsideBorderBrushProperty = DependencyProperty.Register("InsideBorderBrush", typeof(Brush), typeof(TextBoxButton));
        public static DependencyProperty InsideBorderThicknessProperty = DependencyProperty.Register("InsideBorderThickness", typeof(Thickness), typeof(TextBoxButton));
        public static DependencyProperty TextBoxToolTipProperty = DependencyProperty.Register("TextBoxToolTip", typeof(ToolTip), typeof(TextBoxButton));
        public static DependencyProperty ButtonToolTipProperty = DependencyProperty.Register("ButtonToolTip", typeof(ToolTip), typeof(TextBoxButton));

        public TextBoxButton()
        {
            InitializeComponent();
            textBox.TextChanged += new TextChangedEventHandler(TextBoxTextChanged);
        }

        void TextBoxTextChanged(object sender, TextChangedEventArgs e)
        {
            if (!textBox.Text.Contains("\r\n") || ButtonCommand == null) return;
                textBox.Text = textBox.Text.Replace("\r\n", null);
                if (ButtonCommand.CanExecute(textBox.Text))
                    ButtonCommand.Execute(textBox.Text);
        }
        

        public object ButtonContent
        {
            get { return GetValue(ButtonContentProperty); }
            set { SetValue(ButtonContentProperty,value);}
        }

        public ICommand ButtonCommand
        {
            get { return GetValue(ButtonCommandProperty) as ICommand; }
            set { SetValue(ButtonCommandProperty, value); }
        }

        public CornerRadius InsideBorderCornerRadius
        {
            get { return (CornerRadius)GetValue(InsideBorderCornerRadiusProperty); }
            set { SetValue(InsideBorderCornerRadiusProperty, value); }
        }

        public Brush InsideBorderBrush
        {
            get { return (Brush)GetValue(InsideBorderBrushProperty); }
            set { SetValue(InsideBorderBrushProperty, value); }
        }

        public Thickness InsideBorderThickness
        {
            get { return (Thickness) GetValue(InsideBorderThicknessProperty); }
            set { SetValue(InsideBorderThicknessProperty,value); }
        }

        public string Text
        {
            get { return GetValue(TextProperty) as string; }
            set { SetValue(TextProperty, value); }
        }

        public ToolTip TextBoxToolTip
        {
            get { return GetValue(TextBoxToolTipProperty) as ToolTip; }
            set { SetValue(TextBoxToolTipProperty,value); }
        }

        public ToolTip ButtonToolTip
        {
            get { return GetValue(ButtonToolTipProperty) as ToolTip; }
            set { SetValue(ButtonToolTipProperty, value); }
        }
        
        public new void Focus()
        {
            textBox.Focus();
        }
    }
}
